package com.bgs.eat.chat.service.Impl;

import cn.hutool.json.JSONObject;
import com.bgs.eat.chat.entity.ChatRecordEntity;
import com.bgs.eat.chat.mapper.ChatRecordMapper;
import com.bgs.eat.chat.service.ChatRecordService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName ChatRecordController
 * @Description TODO 公共聊天室（用户app--我的）
 * @Author G胖
 * @Date 2022-10-17 下午 02：38
 **/
@Service
public class ChatRecordServiceImpl implements ChatRecordService {

    @Resource
    ChatRecordMapper chatRecordMapper;

    /**
     * @Author G胖
     * @Description 查询俩人之间的聊天记录
     * @Date 2022-10-17 下午 03:50
     * @Return java.util.List<com.bgs.eat.chat.entity.ChatRecordEntity>

     */
    @Override
    public List<ChatRecordEntity> getChatRecord(Integer toId, Integer fromId) {
        Example example = new Example(ChatRecordEntity.class);
        example.createCriteria().andEqualTo("fromId",fromId).andEqualTo("toId",toId).orEqualTo("fromId",toId).andEqualTo("toId",fromId);
        return chatRecordMapper.selectByExample(example);
    }

    /**
     * @Author G胖
     * @Description 用户和商家发送消息的时候将消息存进数据库
     * @Date 2022-10-19 下午 03:48
     * @Return java.lang.Integer

     */
    @Override
    public Integer insertChatRecord(JSONObject data) {
        ChatRecordEntity chatRecordEntity = new ChatRecordEntity();
        chatRecordEntity.setToId(data.getInt("to"));
        chatRecordEntity.setFromId(data.getInt("from"));
        chatRecordEntity.setTime(data.getStr("time"));
        chatRecordEntity.setContent(data.getStr("content"));
        chatRecordEntity.setNickName(data.getStr("nickName"));
        chatRecordEntity.setMsgType(data.getStr("msgType"));
        chatRecordEntity.setAvatarId(data.getInt("avatarId"));
        return chatRecordMapper.insertSelective(chatRecordEntity);
    }

    /**
     * @Author G胖
     * @Description 查询商家给那些用户发送了消息
     * @Date 2022-10-24 下午 01:40
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public List<ChatRecordEntity> getUserChatRecord(Integer toId) {
        List<ChatRecordEntity>chatRecordEntities =chatRecordMapper.getUserChatRecord(toId);
        if(chatRecordEntities!=null){
            for (ChatRecordEntity chatRecordEntity : chatRecordEntities){
                ChatRecordEntity recordEntities = chatRecordMapper.getUserToChatRecord(chatRecordEntity.getToId(),chatRecordEntity.getFromId());
                // 如果对方没有发送消息，则数据不用覆盖及其他操作
                if(recordEntities==null){
                    System.out.println("没有消息交互");
                }
                // 如果id大于的话说明第二次的全查为最新的历史消息，将第二次查询的结果替换斤charRecordEntites里
                else if(recordEntities.getId()>chatRecordEntity.getId()){
                    Collections.replaceAll(chatRecordEntities, chatRecordEntity, recordEntities);
                }
                // 如果不是则将查询的用户头像id，昵称替换
                else{
                    chatRecordEntity.setAvatarId(recordEntities.getAvatarId());
                    chatRecordEntity.setNickName(recordEntities.getNickName());
                    chatRecordEntity.setFromId(recordEntities.getFromId());
                }
            }
        }
        return chatRecordEntities;
    }

    /**
     * @Author G胖
     * @Description 查询用户给哪些商家发送了消息
     * @Date 2022-10-25 上午 08:53
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public List<ChatRecordEntity> getBusinessChatRecord(Integer toId) {
        List<ChatRecordEntity>chatRecordEntities =chatRecordMapper.getBusinessChatRecord(toId);
        if(chatRecordEntities!=null){
            for (ChatRecordEntity chatRecordEntity : chatRecordEntities){
                ChatRecordEntity recordEntities = chatRecordMapper.getBusinessToChatRecord(chatRecordEntity.getToId(),chatRecordEntity.getFromId());
                // 如果对方没有发送消息，则数据不用覆盖及其他操作
                if(recordEntities==null){
                    System.out.println("没有消息交互");
                }
                // 如果id大于的话说明第二次的全查为最新的历史消息，将第二次查询的结果替换斤charRecordEntites里
                else if(recordEntities.getId()>chatRecordEntity.getId()){
                    Collections.replaceAll(chatRecordEntities, chatRecordEntity, recordEntities);
                }
                // 如果不是则将查询的用户头像id，昵称替换
                else{
                    chatRecordEntity.setAvatarId(recordEntities.getAvatarId());
                    chatRecordEntity.setNickName(recordEntities.getNickName());
                    chatRecordEntity.setFromId(recordEntities.getFromId());
                }
            }
        }
        return chatRecordEntities;
    }
}
